package com.poposchool.project.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.poposchool.project.service.IStoreProductService;
import com.poposchool.project.entity.StoreProduct;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author wangjie
 * @since 2022-09-09
 */
@RestController
@RequestMapping("/storeProduct")
public class StoreProductController {

    @Resource
    private IStoreProductService storeProductService;

    // 新增或者更新
    @PostMapping
    public boolean save(@RequestBody StoreProduct storeProduct) {
        return storeProductService.saveOrUpdate(storeProduct);
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return storeProductService.removeById(id);
    }

    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody Map<String,List<Integer>> ids) {
        return storeProductService.removeByIds(ids.get("ids"));
    }

    @GetMapping
    public List<StoreProduct> findAll() {
        return storeProductService.list();
    }

    @GetMapping("/{id}")
    public StoreProduct findOne(@PathVariable Integer id) {
        return storeProductService.getById(id);
    }

    @GetMapping("/page")
    public Page<StoreProduct> findPage(@RequestParam Integer pageNum,
                                       @RequestParam Integer pageSize,
                                       @RequestParam(defaultValue = "") String goodName) {
        QueryWrapper<StoreProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("store_name",goodName);
        queryWrapper.orderByDesc("id");
        return storeProductService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

}

